package org.mspring.mlog.web.module.web.ucenter.admin;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.PostCatalog;
import org.mspring.mlog.service.PostCatalogService;
import org.mspring.mlog.web.query.PostCatalogQueryCriterion;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Sort;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月4日
 */
@Widget
@RequestMapping("/u/{username}/admin/post/catalog")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class UCenterAdmin_PostCatalogWidget extends Abstract_UCenterAdminWidget {
    @Autowired
    private PostCatalogService postCatalogService;

    @RequestMapping("/manage")
    public String manage(@ModelAttribute Page<PostCatalog> postCatalogPage, @ModelAttribute PostCatalog postCatalog, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (postCatalogPage == null) {
            postCatalogPage = new Page<PostCatalog>();
        }
        postCatalogPage.setPageSize(8);
        postCatalogPage.setSort(new Sort("id", Sort.DESC));
        if (postCatalog == null) {
            postCatalog = new PostCatalog();
        }
        queryParams.put("user.id", spaceUser.getId());
        postCatalogPage = postCatalogService.listPage(new PostCatalogQueryCriterion(queryParams), postCatalogPage);
        model.addAttribute("postCatalogPage", postCatalogPage);
        return "skin:/ucenter/admin/post/catalog/manage";
    }

    @RequestMapping("/create/do")
    public String create(@ModelAttribute PostCatalog postCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        postCatalog.setUser(spaceUser);
        postCatalogService.save(postCatalog);
        return String.format("redirect:/u/%s/admin/post/catalog/manage", spaceUser.getName());
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        postCatalogService.safeRemove(id, spaceUser.getId());
        return String.format("redirect:/u/%s/admin/post/catalog/manage", spaceUser.getName());
    }

    @RequestMapping("/edit")
    public String edit(@RequestParam(required = false) Long id, @ModelAttribute PostCatalog postCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            return prompt(model, "请选择要修改的分类");
        }
        postCatalog = postCatalogService.get(id);
        if (postCatalog == null) {
            return prompt(model, "没找到要修改的分类");
        }
        if (postCatalog.getUser().getId() != spaceUser.getId()) {
            return prompt(model, "无权修改");
        }
        model.addAttribute("postCatalog", postCatalog);
        return "skin:/ucenter/admin/post/catalog/edit";
    }

    @RequestMapping("/edit/do")
    public String edit_do(@ModelAttribute PostCatalog postCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        postCatalog.setUser(spaceUser);
        postCatalogService.update(postCatalog);
        return String.format("redirect:/u/%s/admin/post/catalog/manage", spaceUser.getName());
    }
}
